from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SPUSpecification, SPU
from meiduo_admin.serializers.specs import SPUSpecificationSerializer, SPUSerializer
from meiduo_admin.utils import PageNum


class SpecsView(ModelViewSet):
    """
    商品规格表的增删改查
    """
    # 指定序列化器
    serializer_class = SPUSpecificationSerializer
    # 指定查询集
    queryset = SPUSpecification.objects.all()
    # 指定分页器
    pagination_class = PageNum
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    # 自定义方法获取spu信息
    def simple(self, request):
        # 获取数据
        spus = SPU.objects.all()
        # 序列化返回数据
        ser = SPUSerializer(spus, many=True)
        return Response(ser.data)

